﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class RoleChoice extends MovieClip
	{
		public static const NAME:String = "Role-Choice";

		private var sourceServer:String;
		private var effectVol:int = 100;
		
		private var roleData:Object;
		private var roleID:int;
		
		public function RoleChoice(roleInfo:Object, vol:Number, src:String)
		{
			sourceServer = src;
			effectVol = vol;
			
			roleData = roleInfo;
			
			btnRole.addEventListener(MouseEvent.CLICK, onClick);
			btnDelete.addEventListener(MouseEvent.CLICK, onClick);
			btnCreate.addEventListener(MouseEvent.CLICK, onClick);
			
			txtName.selectable = txtStyleName.selectable = txtInfo.selectable = false;
			txtName.mouseEnabled = txtStyleName.mouseEnabled = txtInfo.mouseEnabled = false;
			
			var Stamina:int;
			var Energy:int;
			var Force:int;
			var Intelligence:int;
			var Command:int;
			var Politics:int;
			
			roleID = int(roleInfo["Role_ID"]);
			
			if (roleID == 0)
			{
				btnRole.visible = false;
				btnDelete.visible = false;
			}
			else
			{
				btnCreate.visible = false;
				
				for (var key:String in roleInfo)
				{
					switch(key)
					{
						case "RoleName":
							txtName.text = roleInfo[key];
							break;
						case "StyleName":
							txtStyleName.text = roleInfo[key];
							break;
						case "Stamina":
							Stamina = roleInfo[key];
							break;
						case "Energy":
							Energy = roleInfo[key];
							break;
						case "Force":
							Force = roleInfo[key];
							break;
						case "Intelligence":
							Intelligence = roleInfo[key];
							break;
						case "Command":
							Command = roleInfo[key];
							break;
						case "Politics":
							Politics = roleInfo[key];
							break;
						case "ImageID":
							var faceloader:Loader = new Loader();
							faceloader.load(new URLRequest(sourceServer + "/SanGuo/role/face/" + roleInfo[key] + ".png"));
							faceloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFaceLoad);
							break;
					}
				}
				
				txtInfo.htmlText = "体力：" + Stamina + " 精力：" + Energy + "<br>";
				txtInfo.htmlText += "武力：" + Force + " 智力：" + Intelligence + "<br>";
				txtInfo.htmlText += "统率：" + Command + " 政治：" + Politics + "";
			}
		}

		private function _onFaceLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onFaceLoad);
			
			evt.target.content.x = 19;
			evt.target.content.y = 23;
			
			this.addChild(evt.target.content);
		}

		private function _onRemoved(evt:Event):void
		{
			btnRole.removeEventListener(MouseEvent.CLICK, onClick);
			btnDelete.removeEventListener(MouseEvent.CLICK, onClick);
			btnCreate.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnRole":
					dispatchEvent(new SwcEvent(NAME, ["-roleLogin", roleID], true));
					break;
				case "btnDelete":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleDelete", roleData]], true));
					break;
				case "btnCreate":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleCreate"]], true));
					break;
			}
		}
	}
}